package scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum TokenType {
	/*
	 * Our tokens start with symbols because they should be the quickest
	 * to test, and the longer and more complex should be the most difficult.
	 * This ordering also imposes a scanning "order-of-ops" in case a piece of
	 * text could be scanned as more than one type of token.
	 * */
	LEFT_BRACE ("{"),
	RIGHT_BRACE ("}"),
	LEFT_PAREN ("("),
	RIGHT_PAREN (")"),
	SEMICOLON (";"),
	COMMA (","),
	PIPE ("|"),
	STAR ("*"),
	PLUS ("+"),
	QUESTION_MARK ("?"),
	HASH ("#"),
	BEGIN ("BEGIN"),
	PRINT ("print"),
	SUBSTRING ("substring"),
	REPLACE ("replace"),
	INSERT ("insert"),
	REMOVE ("remove"),
	LENGTH ("length"),
	FOR ("for"),
	WHILE ("while"),
	END ("END"),
	LINE ("LINE"),
	EOL ("EOL"),
	QUOTED_STRING (""),
	CHARACTER (""),
	INTEGER (""),
	VARIABLE (""),
	START (""),
	ERROR (""),
	CONCAT (""),
	REGEX ("");
	
	public static final TokenType[] RESERVED = {BEGIN,END,PRINT,SUBSTRING,REPLACE,INSERT,REMOVE,LENGTH,FOR,WHILE,LINE,EOL,REGEX},
			SPECIAL = {LEFT_BRACE,RIGHT_BRACE, LEFT_PAREN, RIGHT_PAREN, SEMICOLON, COMMA, PIPE, STAR, PLUS, QUESTION_MARK, HASH, CONCAT};
	
	private final String stringRep;
	
	TokenType(String s)
	{
	    stringRep = s;
	}
	
	public static TokenType reserved(String s)
	{
	    for (TokenType t : TokenType.RESERVED)
	        if(t.toString().equals(s))
	            return t;
	    return null;
	}
	
	public static TokenType beginReserved(String s)
	{
	    for (TokenType t : TokenType.RESERVED)
            if(t.toString().startsWith(s))
                return t;
        return null;
	}
	
	public static TokenType special(String s)
    {
        for (TokenType t : TokenType.SPECIAL)
            if(t.getStringRep().equals(s))
                return t;
        return null;
    }
	
	public String getStringRep()
	{
		return stringRep;
	}
}